/**
 * Copyright (C) 2015-2018 IQIYI
 * All rights reserved.
 *
 * Author     : 王海斌
 * E-mail     : wanghaibin@qiyi.com
 * Version    :
 * Date       : Fri 04 Jan 2019 10:48:53 AM CST
 * Destription:
 *
 */
#include <stdio.h>
#include <string.h>
#include <string>

#include <iostream>
#include <sstream>


using namespace std;

static const char * g_char= NULL;
void test_static(const char * xxx) {
    if(NULL!=g_char) {
        printf("before data=%s\n", g_char);
    }
    g_char = xxx;
    printf("after data=%s\n", g_char);
}

void CalculateData(const char * data, size_t count, const std::string &keys, size_t keyPos)
{
    const char * ptr = data;
    size_t kLen = keys.length();
    char * data_ = new char[count];
    char * data2_ = new char[count];
    for(size_t i=0; i<count; i++, ptr++) {
        size_t curPos = (i+keyPos)%kLen;
        data_[i] =  ~(*ptr)^keys[curPos];
            char cnew = data_[i];
            char cold = *ptr;
            char xx = ~(*ptr)^keys[curPos];
            printf("@@@@ data=%d, newdata=%d, curPos=%zd, kes=%s, cc=%c, xx=%d\n", cold, cnew , curPos, keys.c_str(), keys[curPos], xx);
    }

    printf("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, data=%s, data_=%s\n", data, data_);

    ptr = data_;
    for(size_t i=0; i<count; i++, ptr++) {
        size_t curPos = (i+keyPos)%kLen;
        data2_[i] =  ~(*ptr)^keys[curPos];
            char cnew = data2_[i];
            char cold = *ptr;
            char xx = ~(*ptr)^keys[curPos];
            printf("@@@@ data=%d, newdata=%d, curPos=%zd, kes=%s, cc=%c, xx=%d\n", cold, cnew , curPos, keys.c_str(), keys[curPos], xx);
    }
    printf("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, data=%s, data_=%s\n", data_, data2_);
}

class TestCL
{
public:
    TestCL (){};
    ~TestCL (){};

private:
    int a;
    std::string b;
    std::string c;
};


void test_osstream()
{
    std::ostringstream oss;
    oss<< 32 << "aaa" << "\n";
    std::string tmp = oss.str();
    printf("mm=%s, size=%zd\n", tmp.c_str(), tmp.size());

    int i=0;
    do {
        if(i<10) {
            printf("in i=%d\n", i++);
            continue;
        } else {
            break;
        }
        printf("out i=%d\n", i);
    }while(1);
}

void test_sizeof_parse_array(char * tmp, int i) {
    printf("sizeof(tmp)=%d, i=%d\n", sizeof(tmp), i);

}

void test_sizeof()
{
    char tmp[256];
    printf("sizeof(tmp)=%d\n", sizeof(tmp));
    int i=0;
    test_sizeof_parse_array(tmp, ++i);
    printf("i=%d\n",i);
}

int main(int argc, char *argv[])
{

    const char * m = "mmmmmmmm";
    test_static(m);
    const char * p = "pppppp";
    test_static(p);

    return 0;
    test_sizeof();
    return 0;
    test_osstream();
    return 0;
    cout << "len=" << sizeof(TestCL) << endl;
    return 0;

    std::string aaaaaaa("1");
    cout << "len=" << aaaaaaa.size() << endl;
    return 0;
    //const char * ddd="aaaaaaaa";
    //const char * keys="12345";
    //CalculateData(ddd, strlen(ddd), keys, 2);
    char datac = 'F';
    char keyt='/';
    char res = ~(datac)^keyt;
    printf("data=%d key=%c res=%d", datac, keyt, res);

    return 0;
    const char * data="abcdefg";
    const char * key="1234567890;";
    char * data2 = new char[strlen(data)+1];
    const char * ptr = data;
    int keyLen = strlen(key);
    for(int i=0; i<strlen(data); ++i, ptr++) {
       int curPos = i%keyLen;
        data2[i] = ~*ptr^key[curPos];
        //data2[i] = ~~(*ptr^key[curPos]^key[curPos]);
    }
    ptr = data2;
    for(int i=0; i<strlen(data); ++i, ptr++) {
       int curPos = i%keyLen;
        data2[i] = ~*ptr^key[curPos];
    }
    printf("a=%s, b=%s\n", data, data2);
    return 0;
}
